package com.xie.test.time;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author dmm
 * @since 2020-07-03
 */
public class DateTimeUtil {

    public static final DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    public static final DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static final DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyMMdd");
    public static final DateTimeFormatter dateTimeFormatter5 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("UTC"));
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Tokyo"));
    DateTimeFormatter FORMATTER_UTC_TO_TOKYO = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneId.of("Asia/Tokyo"));
    public static final DateTimeFormatter DATE_TIME_FORMATTER_YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DATE_TIME_FORMATTER_YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static DateTimeFormatter TAISEI_DATE_TIME_TOKYO_FORMAT_PATTERN = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.of("Asia/Tokyo"));

    public static DateTimeFormatter TAISEI_YEAR_YY_TOKYO_FORMAT_PATTERN = DateTimeFormatter.ofPattern("YY").withZone(ZoneId.of("Asia/Tokyo"));

    public static DateTimeFormatter TAISEI_MONTH_MM_TOKYO_FORMAT_PATTERN = DateTimeFormatter.ofPattern("MM").withZone(ZoneId.of("Asia/Tokyo"));

    public static DateTimeFormatter TAISEI_MONTH_YYYY_MM_DD_TOKYO_FORMAT_PATTERN = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("Asia/Tokyo"));

    /**
     * @return 日志输出当前时间  格式:yyyy-MM-dd HH:mm:ss
     */
    public static ZonedDateTime now() {
        return ZonedDateTime.now().withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
    }


    public static LocalDate parseLocalDate(String dateTime, DateTimeFormatter dateTimeFormatter) {
        return LocalDate.parse(dateTime, dateTimeFormatter);
    }


    public static ZonedDateTime parseZonedDateTime(String dateTime, DateTimeFormatter dateTimeFormatter) {
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, dateTimeFormatter);
        return zonedDateTime;
    }

    public static String formatZonedDateTime(ZonedDateTime zonedDateTime, DateTimeFormatter dateTimeFormatter) {
        return zonedDateTime.format(dateTimeFormatter);
    }

    public static String formatLocalDate(LocalDate localDate, DateTimeFormatter dateTimeFormatter) {
        return localDate.format(dateTimeFormatter);
    }

    /**
     * @param dateStr
     * @param
     * @return 日期格式转换
     */
    public static LocalDate parseLocalDate(String dateStr) {

        try {
            return LocalDate.parse(dateStr, DATE_TIME_FORMATTER_YYYY_MM_DD);
        } catch (Exception e) {
            try {
                return LocalDate.parse(dateStr, DATE_TIME_FORMATTER_YYYYMMDD);
            } catch (Exception e2) {
                throw new RuntimeException(String.format("date format error:%s", dateStr));
            }

        }
    }


}
